package com.autoai.animation.demo;

import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Wangjw
 * @date 2020-12-17
 * @brief
 */
public class WallpaperControl {

    private static WallpaperControl INSTANCE;

    public static WallpaperControl getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new WallpaperControl();
        }
        return INSTANCE;
    }

    public WallpaperControl() {
        uriList.clear();
        uriList.add(videoUri1);
        uriList.add(videoUri2);
        uriList.add(videoUri3);
        uriList.add(videoUri4);
        uriList.add(videoUri5);
    }

    private CustomVideoView customVideoView;
    private Uri currentUri;
    private List<Uri> uriList = new ArrayList<Uri>();
    private static final Uri videoUri1 = Uri.parse("android.resource://com.autoai.animation.demo" + "/" + R.raw.cloud);
    private static final Uri videoUri2 = Uri.parse("android.resource://com.autoai.animation.demo" + "/" + R.raw.rain);
    private static final Uri videoUri3 = Uri.parse("android.resource://com.autoai.animation.demo" + "/" + R.raw.snow);
    private static final Uri videoUri4 = Uri.parse("android.resource://com.autoai.animation.demo" + "/" + R.raw.sunshine);
    private static final Uri videoUri5 = Uri.parse("android.resource://com.autoai.animation.demo" + "/" + R.raw.night);


    public void setCustomVideoView(CustomVideoView customVideoView) {
        this.customVideoView = customVideoView;
    }

    public void changeAnim() {
        customVideoView.stopPlayback();
        int index = uriList.indexOf(currentUri);
        index++;
        if (index < uriList.size()) {
            currentUri = uriList.get(index);
        } else {
            currentUri = uriList.get(0);
        }
        initVideoView();
    }

    /**
     * 播放视频
     */
    public void initVideoView() {
        if (customVideoView == null) {
            return;
        }
        customVideoView.setVisibility(View.VISIBLE);
        if (currentUri == null) {
            currentUri = videoUri1;
        }
        //播放路径
        customVideoView.setVideoURI(currentUri);
        //播放
        customVideoView.setFocusable(false);
        customVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                    @Override
                    public boolean onInfo(MediaPlayer mp, int what, int extra) {
                        customVideoView.setBackgroundColor(Color.TRANSPARENT);
                        return true;
                    }
                });
            }
        });
        //循环播放
        customVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                customVideoView.start();
            }
        });
        customVideoView.start();
    }

    public boolean isPlaying() {
        if (customVideoView == null) {
            return false;
        } else {
            return customVideoView.isPlaying();
        }
    }

    public void stopVideoView() {
        if (customVideoView == null) {
            return;
        }
        customVideoView.stopPlayback();
        customVideoView.setVisibility(View.GONE);
    }
}
